﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HuiZe.education.View
{
    /// <summary>
    /// SelectChapterView.xaml 的交互逻辑
    /// </summary>
    public partial class NextChapterView3 : HuiZeWindow
    {
        public PlanLessonViewModel planModel { get; set; }

        public NextChapterView3()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height + 10;
            this.MaxWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width + 10;
            Loaded += ResourceView_Loaded;
            PART_Back.Click += PART_Back_Click;
            PART_OneKey.Click += PART_OneKey_Click;
            this.WindowStyle = WindowStyle.None;
            this.Background = Brushes.Transparent;
            this.ResizeMode = ResizeMode.NoResize;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        }

        void PART_Back_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = new bool?(false);

            SelectChapterView resourceView = new SelectChapterView() { DataContext = planModel };
            resourceView.Owner = App.Current.MainWindow;
            resourceView.ShowDialog();
            if (resourceView.ShowDialogResult)
            {

            }
        }

        void ResourceView_Loaded(object sender, RoutedEventArgs e)
        {
            _is_selected_default = false;
            _is_selected_default2 = false;
            _list_selected_res = new HashSet<int>();
            _list_tag = new List<resCourseTagChildModel>();
            _list_res = new List<resCourseListChildModel>();
            PART_CheckAll.Checked += PART_CheckAll_Checked;
            PART_CheckAll.Unchecked += PART_CheckAll_Unchecked;
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            planModel = planLessoniewModel;
            planModel.zjkjList2 = new List<resCourseListChildModel>();
            planLessoniewModel.nextChapterView = this;
            _list_tag = planLessoniewModel.GetzjTagsList();
            PART_CoursesList.ItemsSource = _list_tag;
        }

        void PART_OneKey_Click(object sender, RoutedEventArgs e)
        {
            if (_list_tag == null || _list_tag.Count == 0) return;

            try
            {
                planModel.is_selected_one_reference = true;
                _list_selected_res = new HashSet<int>();
                PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
                foreach (var _tag in _list_tag)
                {
                    var _list_res = planLessoniewModel.GetzjResourcesCourseList(_tag.tag_name);
                    if (_list_res != null && _list_res.Count > 0)
                    {
                        foreach (var _res in _list_res)
                        {
                            if (_res.one_reference == "1")
                            {
                                if(_list_selected_res.Add(_res.id))
                                {
                                    planModel.zjkjList2.Add(_res);
                                }
                            }
                        }
                    }
                }
                planLessoniewModel.SeletedResCount = _list_selected_res.Count;

                if (_cache_res_item_cb != null)
                {
                    foreach (var _cb in _cache_res_item_cb)
                    {
                        _cb.IsChecked = new bool?(_list_selected_res.Contains((int)_cb.Tag));
                    }
                }
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void PART_CoursesItem_Loaded(object sender, RoutedEventArgs e)
        {
            var _rb = sender as RadioButton;
            if (!string.IsNullOrEmpty(planModel.CurrentCategoryName))
            {
                if (_rb.Content.ToString() == planModel.CurrentCategoryName)
                    _rb.IsChecked = new bool?(true);
            }
            else
            {
                if (!_is_selected_default)
                {
                    _is_selected_default = true;
                    _rb.IsChecked = new bool?(true);
                }
            }
        }

        void PART_CheckAll_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_is_auto_selectall) return;
            if (_cache_res_item_cb == null || _cache_res_item_cb.Count == 0) return;

            _cache_res_item_cb.ForEach(x => x.IsChecked = false);
        }

        void PART_CheckAll_Checked(object sender, RoutedEventArgs e)
        {
            if (_is_auto_selectall) return;
            if (_cache_res_item_cb == null || _cache_res_item_cb.Count == 0) return;

            _cache_res_item_cb.ForEach(x => x.IsChecked = true);
        }

        void PART_CoursesItem_Checked(object sender, RoutedEventArgs e)
        {
            _cache_res_item_cb = new List<CheckBox>();
            PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            planLessoniewModel.CurrentCategoryName = ((RadioButton)sender).Content.ToString();
            _list_res = planLessoniewModel.GetzjResourcesCourseList(planLessoniewModel.CurrentCategoryName);

            _is_selected_default2 = false;
            PART_CoursesItemList.ItemsSource = _list_res;
        }

        void PART_IsSelected_CoursesResourceItem_Loaded(object sender, RoutedEventArgs e)
        {
            var _cb_res_item = sender as CheckBox;
            _cb_res_item.Checked += (s, args) =>
            {
                var _model = ((CheckBox)sender).DataContext as resCourseListChildModel;
                if (_list_selected_res.Add(_model.id))
                {
                    planModel.SeletedResCount++;
                    planModel.zjkjList2.Add(_model);
                }
                args.Handled = true;
                RefreshSelectAllStatus();
            };
            _cb_res_item.Unchecked += (s, args) =>
            {
                var _model = ((CheckBox)sender).DataContext as resCourseListChildModel;
                if (_list_selected_res.Contains(_model.id))
                {
                    _list_selected_res.Remove(_model.id);
                    planModel.SeletedResCount--;
                    planModel.zjkjList2.Remove(_model);
                }
                args.Handled = true;
                RefreshSelectAllStatus();
            };
            if (planModel.is_selected_one_reference)
            {
                if (_list_selected_res != null && _list_selected_res.Count > 0)
                {
                    var _model = _cb_res_item.DataContext as resCourseListChildModel;
                    if (_list_selected_res.Contains(_model.id))
                    {
                        _cb_res_item.IsChecked = new bool?(true);
                    }
                }
            }
            _cache_res_item_cb.Add(_cb_res_item);

            if(!_is_selected_default2)
            {
                _is_selected_default2 = true;
                ((RadioButton)((Grid)_cb_res_item.Parent).Children[0]).IsChecked = new bool?(true);
                //_cb_res_item.IsChecked = new bool?(true);
            }
        }

        void PART_CoursesResourceItem_Checked(object sender, RoutedEventArgs e)
        {
            PART_Ethan_Media_Host.SetData(((RadioButton)sender).DataContext as resCourseListChildModel, FullScreen);
            e.Handled = true;
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void Updata(string content)
        {
            //PlanLessonViewModel planLessoniewModel = DataContext as PlanLessonViewModel;
            //var list = planLessoniewModel.GetzjResourcesCourseList(content);
            //kJWrap.Children.Clear();
            //foreach (var item in list)
            //{
            //    //WrapPanel stack = new WrapPanel
            //    //{
            //    //    Width = 300,
            //    //    Height = 30,
            //    //    HorizontalAlignment = HorizontalAlignment.Center,
            //    //    VerticalAlignment = VerticalAlignment.Top,
            //    //    Orientation = Orientation.Horizontal,
            //    //};
            //    YCCheckBox chk = new YCCheckBox
            //    {
            //        Tag = item,
            //        Content = item.resources_name,
            //        Height = 30,
            //        FontSize = 24,
            //        HorizontalAlignment = HorizontalAlignment.Stretch,
            //        VerticalAlignment = VerticalAlignment.Top,
            //        Margin = new Thickness(0, 0, 0, 0),
            //        Template = (ControlTemplate)FindResource("YCCheckBoxTemplate"),
            //        BorderBrush = Brushes.Cyan,
            //        ImageHeight = 16,
            //        IsChecked = true,
            //        ImageWidht = 16,
            //        NomalImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/记住密码.png", UriKind.RelativeOrAbsolute)),
            //        SelectedImageSource = new BitmapImage(new Uri("pack://application:,,,/HuiZe.education.Resource;component/Images/Login/chk_no.png", UriKind.RelativeOrAbsolute)),
            //        YCBorderThickness = new Thickness(2),
            //    };
            //    chk.Click += YCCheckBox_Checked;

            //    //Label lb = new Label
            //    //{
            //    //    Height = 30,
            //    //    Width = 80,
            //    //    Content = item.res_type_text,
            //    //    Margin = new Thickness(3, 0, 0, 0),
            //    //    HorizontalAlignment = HorizontalAlignment.Left,
            //    //    VerticalAlignment = VerticalAlignment.Top,
            //    //};
            //    //stack.Children.Add(chk);
            //    //stack.Children.Add(lb);
            //    //ResroucesItem.Children.Add(stack);
            //    kJWrap.Children.Add(chk);
            //}
        }

        private void CustomRadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            CustomRadioButton btn = sender as CustomRadioButton;
            string type = btn.Content.ToString();
            Updata(type);
        }

        private void YCButton_Click_2(object sender, RoutedEventArgs e)
        {

        }

        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void FullScreen(bool _is_full)
        {
            if (_is_full)
            {
                Topmost = true;
                PART_Root_Border.Margin = new Thickness(0, 0, 0, 0);
                PART_LayoutRoot.ColumnDefinitions[0].Width = new GridLength(0);
                PART_LayoutRoot.ColumnDefinitions[2].Width = new GridLength(0);
                PART_Root_Content.RowDefinitions[0].Height = new GridLength(0);
                PART_Root_Content.RowDefinitions[1].Height = new GridLength(0);
                PART_Root_Content.RowDefinitions[3].Height = new GridLength(0);
                PART_Root_Content.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                PART_Root_Content.ColumnDefinitions[1].Width = new GridLength(0);

            }
            else
            {
                Topmost = false;
                PART_Root_Border.Margin = new Thickness(0, 35, 0, 35);
                PART_LayoutRoot.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                PART_LayoutRoot.ColumnDefinitions[2].Width = new GridLength(1, GridUnitType.Star);
                PART_Root_Content.RowDefinitions[0].Height = new GridLength(90);
                PART_Root_Content.RowDefinitions[1].Height = new GridLength(80);
                PART_Root_Content.RowDefinitions[3].Height = new GridLength(80);
                PART_Root_Content.ColumnDefinitions[0].Width = new GridLength(880, GridUnitType.Star);
                PART_Root_Content.ColumnDefinitions[1].Width = new GridLength(220, GridUnitType.Star);
            }
        }

        void RefreshSelectAllStatus()
        {
            if (_cache_res_item_cb == null || _cache_res_item_cb.Count == 0) return;

            int _count_selected = 0;
            foreach (var _cb in _cache_res_item_cb)
            {
                if (_cb.IsChecked == true)
                    _count_selected++;
            }
            if (_count_selected == 0)
            {
                if (PART_CheckAll.IsChecked == true)
                {
                    _is_auto_selectall = true;
                    PART_CheckAll.IsChecked = new bool?(false);
                }
            }
            if (_count_selected == _cache_res_item_cb.Count)
            {
                if (PART_CheckAll.IsChecked != true)
                {
                    _is_auto_selectall = true;
                    PART_CheckAll.IsChecked = new bool?(true);
                }
            }
            if (_is_auto_selectall)
            {
                Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
                {
                    _is_auto_selectall = false;
                }));
            }
        }

        bool _is_auto_selectall;
        bool _is_selected_default;
        bool _is_selected_default2;
        HashSet<int> _list_selected_res;
        List<CheckBox> _cache_res_item_cb;
        List<resCourseTagChildModel> _list_tag;
        List<resCourseListChildModel> _list_res;

    }
}
